package io.renren.modules.demo.controller;

import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.demo.dto.ChildSixData;
import io.renren.modules.demo.dto.DhOutdorsportInfoDTO;
import io.renren.modules.demo.excel.DhOutdorsportInfoExcel;
import io.renren.modules.demo.service.DhOutdorsportInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2023-03-25
 */
@RestController
@RequestMapping("demo/dhoutdorsportinfo")
@Api(tags = "")
public class DhOutdorsportInfoController {
    @Autowired
    private DhOutdorsportInfoService dhOutdorsportInfoService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String")
    })
    @RequiresPermissions("demo:dhoutdorsportinfo:page")
    public Result<PageData<DhOutdorsportInfoDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<DhOutdorsportInfoDTO> page = dhOutdorsportInfoService.page(params);

        return new Result<PageData<DhOutdorsportInfoDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("demo:dhoutdorsportinfo:info")
    public Result<DhOutdorsportInfoDTO> get(@PathVariable("id") Long id) {
        DhOutdorsportInfoDTO data = dhOutdorsportInfoService.get(id);

        return new Result<DhOutdorsportInfoDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("demo:dhoutdorsportinfo:save")
    public Result save(@RequestBody DhOutdorsportInfoDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        dhOutdorsportInfoService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("demo:dhoutdorsportinfo:update")
    public Result update(@RequestBody DhOutdorsportInfoDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        dhOutdorsportInfoService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("demo:dhoutdorsportinfo:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        dhOutdorsportInfoService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("demo:dhoutdorsportinfo:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<DhOutdorsportInfoDTO> list = dhOutdorsportInfoService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, DhOutdorsportInfoExcel.class);
    }

    @PostMapping("sportApply")
    @ApiOperation("活动报名")
    @LogOperation("活动报名")
    public Result sportApply(@RequestBody Map<String, Object> params) {
        Integer sportId = Integer.valueOf(String.valueOf(params.get("sportId")));
        Long userId = Long.valueOf(String.valueOf(params.get("userId")));
        Integer i = dhOutdorsportInfoService.sportApply(sportId, userId);
        return new Result<Integer>().ok(i);
    }

    @PostMapping("sportApplyCancel")
    @ApiOperation("取消报名")
    @LogOperation("取消报名")
    public Result sportApplyCancel(@RequestBody Map<String, Object> params) {
        Integer sportId = Integer.valueOf(String.valueOf(params.get("sportId")));
        Long userId = Long.valueOf(String.valueOf(params.get("userId")));
        Integer i = dhOutdorsportInfoService.sportApplyCancel(sportId, userId);
        return new Result<Integer>().ok(i);
    }

    @PostMapping("newSport")
    @ApiOperation("发起新的户外运动")
    @LogOperation("发起新的户外运动")
    public Result newSport(@RequestBody Map<String, Object> params) {
        String sportName = String.valueOf(params.get("sportName"));
        String sportAddress = String.valueOf(params.get("sportAddress"));
        String startTime = String.valueOf(params.get("startTime"));
        String endTime = String.valueOf(params.get("endTime"));
        Integer upper = Integer.valueOf(String.valueOf(params.get("upper")));
        String sportLeader = String.valueOf(params.get("sportLeader"));
        String traffic = String.valueOf(params.get("traffic"));
        dhOutdorsportInfoService.newSport(sportName, sportAddress, startTime, endTime, upper, sportLeader, traffic);
        return new Result();
    }

    @GetMapping("/getSportInfo/{sportId}")
    @ApiOperation("根据户外运动编号获取运动信息")
    @LogOperation("根据户外运动编号获取运动信息")
    public Result<DhOutdorsportInfoDTO> getSportInfo(@PathVariable("sportId")Integer sportId) {
        DhOutdorsportInfoDTO data = dhOutdorsportInfoService.getSportInfo(sportId);
        return new Result<DhOutdorsportInfoDTO>().ok(data);
    }

    @GetMapping("/getChildSixData")
    @ApiOperation("获取不同活动报名人数")
    @LogOperation("获取不同活动报名人数")
    public Result<List<ChildSixData>> getChildSixData() {
        List<ChildSixData> data = dhOutdorsportInfoService.getChildSixData();
        return new Result<List<ChildSixData>>().ok(data);
    }

}